<?php

namespace App\Model;

//use Illuminate\Database\Eloquent\Model;

class AdminRole extends Model
{
    public $dates = ['deleted_at'];

    protected $fillable = ['id','admin','role','admin_id','created_at','updated_at'];

    static public function _validate(){
        return array(
            'admin'=>'max:10',
            'role'=>'max:10',
            'admin_id'=>'max:10',
        );
    }

    static public function RoleAdd($admin,$role){
        $data = array('admin'=>$admin,'role'=>$role,'admin_id'=>auth()->user()->id);
        $data = self::validate($data);
        if(!$data || !$data['status']){
            return $data;
        }
        $Role = self::create($data['data']);
        if($Role){
            return Json::success($Role);
        }else{
            return Json::error('添加失败');
        }
    }

    static public function RoleDel($admin,$role){
        $role = self::where('admin',$admin)->where('role',$role)->first();
        if(!$role){
            return Json::error('没有此权限');
        }
        $role->admin_id = auth()->user()->id;
        if($role->save() && $role->delete()){
            return Json::success('删除成功');
        }
        return Json::error('删除失败');

    }

}
